package com.atguigu.cart.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import com.atguigu.cart.interceptor.CartInterceptor;
import com.atguigu.cart.service.CartService;
import com.atguigu.cart.to.UserInfoTo;
import com.atguigu.cart.vo.Cart;
import com.atguigu.cart.vo.CartItem;
import com.atguigu.common.exception.enumtion.ExceptionEnum;
import com.atguigu.common.to.AutoLoginResponse;
import com.atguigu.common.to.UmsMemberEntity;
import com.atguigu.common.utils.R;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**
 * 购物车控制层
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:57:10
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;




    /**
     *远程调用查看购物车已选中的数据--并且价格需要再次查询数据库来获取
     */
    @ResponseBody
    @GetMapping("orderToCart")
    public List<CartItem> orderToCart() {
        List<CartItem> cartItems = this.cartService.orderToCart();
        return cartItems;

    }

    @GetMapping("cartList.html")
    public String cartList(Model model) throws ExecutionException, InterruptedException {
       Cart cart =  cartService.cartList();
        model.addAttribute("cart",cart);

        return "cartList";

    }
    @GetMapping("success.html")
    public String success() {
        return "success";

    }


    /**
     *改变商品的数量
     */
    @GetMapping("countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num) {
        cartService.countItem(skuId, num);
        return "redirect:http://cart.gulimail.com/cartList.html";

    }


    /**
     *改变商品的状态
     */
    @GetMapping("checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check) {
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimail.com/cartList.html";

    }

    /**
     *删除某一购物车里的商品
     */
    @GetMapping("deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimail.com/cartList.html";

    }


    /**
     * 添加到购物车中--重定向到控制层-控制层再次查询返回到success页面可以避免重复提交
     * @return
     */
    @GetMapping("addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num,RedirectAttributes ra) throws ExecutionException, InterruptedException {
        CartItem cartItem =  cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",cartItem.getSkuId());
        return "redirect:http://cart.gulimail.com/addToCartSuccess.html";

    }


    /**
     * 根据商品查询购物车
     * @return
     */
    @GetMapping("addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model){

        CartItem cartItem =  cartService.addToCartSuccess(skuId);
        model.addAttribute("item",cartItem);
        return "success";

    }




    }


